package tech.daoxuan.web.controller;

import com.fasterxml.jackson.annotation.JsonView;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang.builder.ReflectionToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.*;
import tech.daoxuan.web.entity.User;
import tech.daoxuan.web.entity.querycondition.UserCondition;

import javax.validation.Valid;
import java.util.LinkedList;
import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {
    Logger logger = LoggerFactory.getLogger(UserController.class);


//    @GetMapping("/me")
//    public Object getCurrentUser(){
//        return SecurityContextHolder.getContext().getAuthentication();
//    }
//    @GetMapping("/me")
//    public Object getCurrentUser(Authentication authentication){
//        return authentication;
//    }
    @GetMapping("/me")
    public Object getCurrentUser(@AuthenticationPrincipal UserDetails userDetails){
        return userDetails;
    }

    @PostMapping
    public User creat(@Valid @RequestBody User user, BindingResult result){
        if (result.hasErrors()){
            result.getAllErrors().stream().forEach(s -> {
            logger.info(s.toString());
            });
        }
        user.setId("1");
        logger.info("----->>>>"+user.toString());
        return user;
    }




//    @GetMapping("/user")
//    public List<User> getUser(@RequestParam String username){
//        System.out.println(username+"===========");
//        List<User> list=new LinkedList<>();
//        list.add(new User());
//        list.add(new User());
//        list.add(new User());
//        return list;
//    }

    /**
     * 把页面传过来的值封装给一个对象  这种方式可以隐藏真实对象的属性。
     * @param userCondition
     * @return
     */
    //@GetMapping("/user")
    @GetMapping
    @JsonView(User.UserSimpleView.class)
    @ApiOperation(value = "用户查询服务 列表")
    public List<User> getUser(UserCondition userCondition, @PageableDefault() Pageable pageable){
        /**
         * ReflectionToStringBuilder 反射工具类
         */
        System.out.println(ReflectionToStringBuilder.toString(userCondition, ToStringStyle.MULTI_LINE_STYLE));

        List<User> list=new LinkedList<>();
        list.add(new User());
        list.add(new User());
        list.add(new User());
        return list;
    }

    /**
     * @GetMapping  取代 @RequestMapping
     * @param id
     * @return
     */
    //@RequestMapping(value = "/user/{id}",method = RequestMethod.GET)
   // @GetMapping("/user/{id:\\d+}")
    @GetMapping("/{id:\\d+}")
    @JsonView(User.UserDetailView.class)
    public User getInfo(@ApiParam("用户id") @PathVariable(value = "id",required = false) String id){
//        throw new RuntimeException("user is not exit .......");
       // throw new UserNotExitException(id);
        User user = new User();
        user.setName("tom");
        return user;
    }

    @PutMapping("/{id:\\d+}")
    public User update(@Valid @RequestBody User user, BindingResult result){
        if (result.hasErrors()){
            result.getAllErrors().stream().forEach(s -> {
               FieldError fieldError =(FieldError) s;
               logger.info(fieldError.getField()+"\n"+fieldError.getDefaultMessage());
                logger.info(s.toString());
            });
        }
        user.setId("1");
        logger.info("----->>>>"+user.toString());
        return user;
    }

    @DeleteMapping("/{id:\\d+}")
    public void delet(@PathVariable String id){
        logger.info(id+"is deleted -----------");
    }
}
